<template>
	<view class="">

	</view>
</template>

<script>
	export default {
		data() {
			return {

			}
		},
		methods: {
			getLocation() {
				let platform = uni.getSystemInfoSync().platform //手机型号
				if (this.openGps(platform)) {
					let type = platform == 'android' ? 'gcj02' : 'wgs84';
					uni.showLoading({
						title: '获取位置中……'
					});
					uni.getLocation({
						/* 
						 Android端altitude设置为true,要不然定位失败，
						 ios端不需要
						 */
						altitude: platform == 'android' ? true : false,
						type: type,
						geocode: true, //安卓需指定 type 为 gcj02 并配置三方定位SDK。h5端需要逆解析地理位置获取城市
						success: res => {
							console.log(res, '地理位置');
							uni.hideLoading();

							this.location = res.longitude + ',' + res.latitude; //经纬度

							// APP端可以获取详细地址
							// #ifdef APP
							this.city = res.address.city; //城市
							this.address = res.address.poiName + res.address.street + res.address
								.streetNum //详细地址
							// #endif
						},
						fail(err) {
							console.log(err, '获取位置失败了');
							uni.hideLoading();
							if (err.errCode == -1502) {
								plus.nativeUI.toast('网络不畅，请稍后重试。')
								// uni.showToast({
								// 	title: '网络不畅，请稍后重试。',
								// 	icon: 'none'
								// });
							} else {
								plus.nativeUI.toast('获取定位结果错误~')
								// uni.showToast({
								// 	title: '获取定位结果错误~',
								// 	icon: 'none'
								// });
							}
						}
					});
				}
			},
			openGps(platform) {
				// 编译到非app平台返回都是true
				// #ifndef APP
				return true
				// #endif
				// #ifdef APP-PLUS
				if (platform == 'ios') {
					// https://ask.dcloud.net.cn/article/36020
					let cllocationManger = plus.ios.import('CLLocationManager');
					let enable = cllocationManger.locationServicesEnabled();
					let status = cllocationManger.authorizationStatus();
					if (enable && status != 2) {
						console.log('定位开启可以获取用户位置了');
						return true
					} else {
						uni.showModal({
							title: '温馨提示',
							content: '手动开启定位,以便护士小鹿为您提供更好的服务。',
							confirmText: '去设置',
							cancelText: '暂不开启',
							confirmColor: '#007AFF',
							success: function(res) {
								if (res.confirm) {
									let UIApplication = plus.ios.importClass('UIApplication');
									let application2 = UIApplication.sharedApplication();
									let NSURL2 = plus.ios.importClass('NSURL');
									let setting2 = NSURL2.URLWithString('app-settings:'); // 打开应用权限
									application2.openURL(setting2);
									plus.ios.deleteObject(setting2);
									plus.ios.deleteObject(NSURL2);
									plus.ios.deleteObject(application2);
								}
							}
						})
						return false
					}
					plus.ios.deleteObject(cllocationManger);
				} else {
					let context = plus.android.importClass('android.content.Context');
					let locationManager = plus.android.importClass('android.location.LocationManager');
					let main = plus.android.runtimeMainActivity();
					let mainSvr = main.getSystemService(context.LOCATION_SERVICE);
					if (mainSvr.isProviderEnabled(locationManager.GPS_PROVIDER)) {
						console.log('定位开启可以获取用户位置了');
						return true
					} else {
						uni.showModal({
							title: '温馨提示',
							content: '手动开启定位,以便护士小鹿为您提供更好的服务。',
							confirmText: '去设置',
							cancelText: '暂不开启',
							confirmColor: '#007AFF',
							success: function(res) {
								if (res.confirm) {
									var Intent = plus.android.importClass('android.content.Intent');
									var Settings = plus.android.importClass('android.provider.Settings');
									var intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
									main.startActivity(intent); // 打开系统设置GPS服务页面
								}
							}
						})
						return false
					}
				}
				// #endif
			}

		}
	}
</script>
<style lang="less" scoped>

</style>